package gui.specialGUIs;

import gui.designComponents.TMTComboBox;
import gui.designComponents.TMTDefaultFormBuilder;
import gui.designComponents.TMTNormalButton;
import gui.designComponents.TMTPanel;
import gui.designComponents.TMTTextField;

import java.awt.BorderLayout;
import java.util.ResourceBundle;

import app.Controller;
import app.Model;
import bundles.BundleLoader;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import definitions.Commands;

/**
 * Panel to create a Training Plan
 * 
 * @author MK
 * 
 */
public class TrainingPlanPanel extends TMTPanel {

	public TMTComboBox seasonStartMonthBox;
	public TMTComboBox seasonStartDayBox;
	public TMTTextField seasonStartYearField;
	public TMTComboBox seasonEndMonthBox;
	public TMTComboBox seasonEndDayBox;
	public TMTTextField seasonEndYearField;
	public TMTComboBox numberOfTrainingsBox;
	public int numberOfTrainings;
	public TMTComboBox[] dayOfTrainingBox;
	public TMTComboBox[] startOfTrainingBox;
	public DefaultFormBuilder builder;

	private Model model;
	private Controller controller;

	public TrainingPlanPanel(Model model, Controller controller) {

		this.model = model;
		this.controller = controller;

		setLayout(new BorderLayout());

		builder = fillBuilder();
		add(builder.getPanel(), BorderLayout.CENTER);
		add(new TrainingPlanButtonPanel(controller), BorderLayout.SOUTH);
	}

	private DefaultFormBuilder fillBuilder() {

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		seasonStartMonthBox = new TMTComboBox(model.getMonthValues());
		seasonStartDayBox = new TMTComboBox(model.getDaysOfMonthValues());
		seasonStartYearField = new TMTTextField();

		seasonEndMonthBox = new TMTComboBox(model.getMonthValues());
		seasonEndDayBox = new TMTComboBox(model.getDaysOfMonthValues());
		seasonEndYearField = new TMTTextField();

		numberOfTrainingsBox = new TMTComboBox(model.getTrainingDaysValues());
		numberOfTrainingsBox
				.setActionCommand(Commands.SHOW_TRAININGPLAN_CREATE_WITH_SELECTION);
		numberOfTrainingsBox.addActionListener(controller);

		FormLayout layout = new FormLayout(
				"right:max(40dlu;p), 4dlu, 80dlu, 7dlu, "
						+ "right:max(40dlu;p), 4dlu, 80dlu, 7dlu, "
						+ "right:max(40dlu;p), 4dlu, 80dlu", 
						"");

		builder = TMTDefaultFormBuilder.getDFB(layout);

		builder.appendSeparator(resBundle.getString("seasonStart"));
		builder.append(resBundle.getString("year"), seasonStartYearField);
		builder.append(resBundle.getString("month"), seasonStartMonthBox);
		builder.append(resBundle.getString("day"), seasonStartDayBox);
		builder.nextLine();
		builder.appendSeparator(resBundle.getString("seasonEnd"));
		builder.append(resBundle.getString("year"), seasonEndYearField);
		builder.append(resBundle.getString("month"), seasonEndMonthBox);
		builder.append(resBundle.getString("day"), seasonEndDayBox);

		builder.nextLine();

		builder.appendSeparator(resBundle.getString("events"));
		builder.append(resBundle.getString("numberOfTrainings"),
				numberOfTrainingsBox);

		return builder;
	}

	public void appendLines() {

		numberOfTrainings = numberOfTrainingsBox.getSelectedIndex() + 1;
		numberOfTrainingsBox.setEnabled(false);

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		dayOfTrainingBox = new TMTComboBox[numberOfTrainings];
		startOfTrainingBox = new TMTComboBox[numberOfTrainings];

		for (int lineCounter = 0; lineCounter < numberOfTrainings; lineCounter++) {

			dayOfTrainingBox[lineCounter] = new TMTComboBox(model
					.getWeekdaysValues());
			startOfTrainingBox[lineCounter] = new TMTComboBox(model
					.getTimeValues());

			builder.nextLine();
			builder.append(resBundle.getString("day"),
					dayOfTrainingBox[lineCounter]);
			builder.append(resBundle.getString("at"),
					startOfTrainingBox[lineCounter]);
		}
	}

	public void update() {

		remove(0);
		builder = fillBuilder();
		this.add(builder.getPanel(), 0);
	}
}

class TrainingPlanButtonPanel extends TMTPanel {

	public TrainingPlanButtonPanel(Controller controller) {
		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		TMTNormalButton submitButton = new TMTNormalButton(resBundle
				.getString("submitTrainingPlan"));
		submitButton.setActionCommand(Commands.DO_TRAININGPLAN_CREATE);
		submitButton.addActionListener(controller);
		add(submitButton);

		TMTNormalButton backButton = new TMTNormalButton(resBundle
				.getString("back"));
		backButton.setActionCommand(Commands.SHOW_EVENT_OVERVIEW);
		backButton.addActionListener(controller);
		add(backButton);
	}
}